<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['merchname'])){
		$params['merchname like']='%'.trim($_RQ['merchname']).'%';
		$return['params']['merchname']=$_RQ['merchname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','mobile','realname','status','jointime','merchname');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_jd_user',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($return['list'] as &$v){
		$v['jointime']=date('Y/m/d H:i:s',$v['jointime']);
		$v['url']=custom_url('jy_jd/index//mid='.$v['mid'],true);
		
		
	}
	$return['status']=array('0'=>array('id'=>0,'title'=>'待入驻'),'1'=>array('id'=>1,'title'=>'入驻中'),'2'=>array('id'=>2,'title'=>'暂停中'));
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'多商户管理'),
			array('op'=>'edit','title'=>'添加多商户'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'merchname','title'=>'商户名称','type'=>'string'),		
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),					
			array('name'=>'jointime','title'=>'入驻时间','type'=>'string'),			
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status2'),	
			array('name'=>'url','title'=>'入口','type'=>'ewm'),						
			
		),
		
		),
		'search'=>array(
			array('name'=>'merchname','width'=>15,'title'=>'商户名称','type'=>'string'),
			array('name'=>'realname','width'=>15,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'电话','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status'),
		)
	);
	$return['status2']=array('1'=>'入驻中','2'=>'暂停中');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_jd_user',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['upass']='';
    $return['item']=$item;
	$htmls=array( 
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'多商户设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'多商户管理'),
			array('op'=>'edit','title'=>'添加多商户'),
		),
		'edit'=>array(
			array('name'=>'merchname','title'=>'商户名称','type'=>'string'),
			array('name'=>'logo','title'=>'商户logo','type'=>'image'),			
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'uname','title'=>'账户名','type'=>'string'),
			array('name'=>'upass','title'=>'账户密码','type'=>'string'),
			array('name'=>'mid','title'=>'选择管理员','type'=>'choose_title','action'=>'get_member'),					
			array('name'=>'desc','title'=>'商户简介','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'payrate','title'=>'个性抽成利率（%）','type'=>'string','help'=>'不填，默认是平台设置的'),
			array('name'=>'paymid','title'=>'选择收款人','type'=>'choose_title','action'=>'get_member'),					
		)
	);
	$return['status']=array('0'=>'待审核','1'=>'入驻中','2'=>'暂停中');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['merchname']){
		exi('请填写商户名','error');
	}
	if(!$post['mid']){
		exi('请选择管理员','error');
	}
	if(!$post['uname']){
		exi('请填写账户名','error');
	}
	$post['upass']=base64_encode($post['upass']);
	$info=pdo_get('jy_jd_user',array('mid'=>$post['mid'],'uniacid'=>$_SESSION['uniacid']));	
	$uname=pdo_get('jy_jd_user',array('uname'=>$post['uname']));
	if($post['id']){
		if(!$post['upass']){
			unset($post['upass']);
		}
		if($info && $info['id']!=$post['id']){
			exi('一个管理员只能绑定一个账号','error');
		}
		if($uname && $uname['id']!=$post['id']){
			exi('登陆账户名不可以重复','error');	
		}
		pdo_update('jy_jd_user',$post,array('id'=>$post['id']));
	}else{
		if($uname){
			exi('登陆账户名不可以重复','error');	
		}
		if($info){
			exi('一个管理员只能绑定一个账号','error');
		}
		$post['uniacid']=$_SESSION['uniacid'];
		$post['jointime']=time();
		pdo_insert('jy_jd_user',$post);
	}
	exi('保存成功','','list');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jd_user',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jd_user',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
?>